<?PHP
header("Content-type: text/html; charset=utf-8");
include_once "../DB_Oracle.php";
$Db_sql=new DB_Oracle();
if(isset($_POST['option'])&&$_POST['option']=='add'){
	$sql="select * from dep_bound t where t.bound_name='".$_POST['name']."'";
	$count=$Db_sql->fetch_all_array($sql);
	if(count($count)!=0){
		exit("N");
		}
  $sql="insert into dep_bound(bound_index,bound_name,bound_rect,bound_times,bound_timee)values
(dep_bound_seq.nextval,'".$_POST['name']."','".$_POST['points']."',to_date('".$_POST['s']."','yyyy-mm-dd hh24:mi:ss'),to_date('".$_POST['e']."','yyyy-mm-dd hh24:mi:ss'))";
	$is=$Db_sql->query($sql);
	if($is){
		exit("Y");
		}else{
		exit("N");}
	}
if(isset($_POST['option'])&&$_POST['option']=='me'){
	$sql="select * from dep_department t";
	$arr=$Db_sql->fetch_all_array($sql);
	for($i=0;$i<count($arr);$i++){
		echo $arr[$i]['DEPARTMENT_INDEX'].';'.$arr[$i]['DEPARTMENT_NAME']."_";
		}
	exit();
	}
if(isset($_POST['option'])&&$_POST['option']=='addrect'){
	$sql="update dep_bound dp
          set dp.bound_member='".$_POST['a']."_".$_POST['b']."' 
      where dp.bound_name='".$_POST['name']."'";
	 $is=$Db_sql->query($sql);
	 if($is){
	exit("Y");
		 } else{
	exit("N");}
	}
	
if(isset($_POST['option'])&&$_POST['option']=='bound'){
	$sql="select * from dep_bound t";
	$arr=$Db_sql->fetch_all_array($sql);
	if(count($arr)==0){
		exit("N");
		}
	for($i=0;$i<count($arr);$i++){
		echo $arr[$i]['BOUND_INDEX'].';'.$arr[$i]['BOUND_NAME']."_";
		}
	exit();
	}
if(isset($_POST['option'])&&$_POST['option']=='addedit'){
$sql="update dep_bound dp
          set dp.bound_member='".$_POST['a']."_".$_POST['b']."' ,
		  dp.bound_times=to_date('".$_POST['d']."','yyyy-mm-dd hh24:mi:ss'),
		  dp.bound_timee=to_date('".$_POST['e']."','yyyy-mm-dd hh24:mi:ss'),
		  dp.bound_rect='".$_POST['c']."' 
      where dp.bound_index=".$_POST['name']."";
	 $is=$Db_sql->query($sql);
	 if($is){
	exit("Y");
		 } else{
	exit("N");}
	}
if(isset($_POST['boundid'])){
	$sql="select * from dep_bound t where bound_index='".$_POST['boundid']."'";
	$arr=$Db_sql->fetch_all_array($sql);
	if(count($arr)!==1){
		exit("N");
		}
	echo $arr[0]['BOUND_RECT'];
	exit();
	}
if(isset($_POST['boundedit'])){
	$sql="select t.bound_member,to_char(t.bound_times,'yyyy-mm-dd hh24:mi:ss') bound_times,to_char(t.bound_timee,'yyyy-mm-dd hh24:mi:ss') bound_timee from dep_bound t where bound_index='".$_POST['boundedit']."'";
	$arr=$Db_sql->fetch_all_array($sql);
	if(count($arr)!==1){
		exit("N");
		}
	  echo $arr[0]['BOUND_MEMBER']."|".$arr[0]['BOUND_TIMES']."|".$arr[0]['BOUND_TIMEE']."|";
	  $c=explode('_',$arr[0]['BOUND_MEMBER']);
	  if($c[0]!=''){
	  $sql="select * from dep_department t1 where t1.department_index in (select t.department_index from dep_vehicle t where t.vehicle_index in (".$c[0]."))";
	  $arr=$Db_sql->fetch_all_array($sql);
	  for($i=0;$i<count($arr);++$i){
		  echo $arr[$i]['DEPARTMENT_INDEX'].",";
		}}
		if($c[1]!=''){
      $sql="select * from dep_department t1 where t1.department_index in (select t.department_index from dep_personnel t where t.personnel_index in (".$c[1]."))";
	  $arr=$Db_sql->fetch_all_array($sql);
	  for($i=0;$i<count($arr);++$i){
		  echo $arr[$i]['DEPARTMENT_INDEX'].",";
		  }}
	exit();
	}
	
if(isset($_POST['del'])){
	$sql="delete dep_bound t where t.bound_index=".$_POST['del']."";
	$arr=$Db_sql->query($sql);
	if($arr){
		exit("Y");
		}else{
			exit("N");
			}
	}
if(isset($_POST['rect'])){
	$sql="select  t.bound_member,to_char(t.bound_times,'yyyy-mm-dd hh24:mi:ss') bound_times,to_char(t.bound_timee,'yyyy-mm-dd hh24:mi:ss') bound_timee  from dep_bound t where bound_index='".$_POST['t']."'";
	$arr=$Db_sql->fetch_all_array($sql);
	if($arr[0]['BOUND_MEMBER']==''){
		exit('N');
		}
	$c=explode('_',$arr[0]['BOUND_MEMBER']);
	if($c[0]==''&&$c[1]==''){
		exit('N');
		}
		$allstr="[";
		if($c[0]!=''){
	$sql="select * from dep_vehicle t where t.vehicle_index in (".$c[0].")";
	$arr1=$Db_sql->fetch_all_array($sql);
	$lishi1=explode(' ',$arr[0]['BOUND_TIMEE']);
	$lishi=explode(' ',$arr[0]['BOUND_TIMES']);
	$times=$_POST['ts']." ".$lishi[1];
	$timee=$_POST['ts']." ".$lishi1[1];
   for($i=0;$i<count($arr1);++$i){
$sql="select t.longitude,t.latitude,to_char(t.recv_time,'yyyy-mm-dd hh24:mi:ss') recv_time 
  from loc_vehicle_0101 t
 where t.device_id = '".$arr1[$i]['DEVICE_ID']."'
   and t.recv_time > to_date('".$times."','yyyy-mm-dd hh24:mi:ss')
   and t.recv_time < to_date('".$timee."','yyyy-mm-dd hh24:mi:ss')  order by t.recv_time";
 $allpoint=$Db_sql->fetch_all_array($sql);
 $allstr.="[{".$arr1[$i]['LICENSE_PLATE'].":".json_encode($allpoint)."}],";
	}}
 $allstr.="[{a:'abc'}],";
 if($c[1]!=''){
	$sql="select * from dep_personnel t where t.personnel_index in (".$c[1].")";
	$arr1=$Db_sql->fetch_all_array($sql);
	$lishi1=explode(' ',$arr[0]['BOUND_TIMEE']);
	$lishi=explode(' ',$arr[0]['BOUND_TIMES']);
	$times=$_POST['ts']." ".$lishi[1];
	$timee=$_POST['ts']." ".$lishi1[1];
   for($i=0;$i<count($arr1);++$i){
$sql="select t.longitude,t.latitude,to_char(t.recv_time,'yyyy-mm-dd hh24:mi:ss') recv_time 
  from loc_personnel_0101 t
 where t.device_id = '".$arr1[$i]['DEVICE_ID']."'
   and t.recv_time > to_date('".$times."','yyyy-mm-dd hh24:mi:ss')
   and t.recv_time < to_date('".$timee."','yyyy-mm-dd hh24:mi:ss')  order by t.recv_time";
 $allpoint=$Db_sql->fetch_all_array($sql);
 $allstr.="[{".$arr1[$i]['PERSONNEL_SN'].":".json_encode($allpoint)."}],";
	}}
echo  substr($allstr,0,strlen($allstr)-1)."]";		
exit();		
		}
		



if(isset($_POST['option'])&&$_POST['option']=='area'){
		$allstr="[";
		if($_POST['a']!=''){
	$sql="select * from dep_vehicle t where t.vehicle_index in (".$_POST['a'].")";
	$arr1=$Db_sql->fetch_all_array($sql);
   for($i=0;$i<count($arr1);++$i){
$sql="select t.longitude,t.latitude,to_char(t.recv_time,'yyyy-mm-dd hh24:mi:ss') recv_time 
  from loc_vehicle_0101 t
 where t.device_id = '".$arr1[$i]['DEVICE_ID']."'
   and t.recv_time > to_date('".$_POST['ts']."','yyyy-mm-dd hh24:mi:ss')
   and t.recv_time < to_date('".$_POST['te']."','yyyy-mm-dd hh24:mi:ss')  order by t.recv_time";
 $allpoint=$Db_sql->fetch_all_array($sql);
 $allstr.="[{".$arr1[$i]['LICENSE_PLATE'].":".json_encode($allpoint)."}],";
	}}
 $allstr.="[{a:'abc'}],";
 if($_POST['b']!=''){
	$sql="select * from dep_personnel t where t.personnel_index in (".$_POST['b'].")";
	$arr1=$Db_sql->fetch_all_array($sql);
   for($i=0;$i<count($arr1);++$i){
$sql="select  t.longitude,t.latitude,to_char(t.recv_time,'yyyy-mm-dd hh24:mi:ss') recv_time 
  from loc_personnel_0101 t
 where t.device_id = '".$arr1[$i]['DEVICE_ID']."'
   and t.recv_time > to_date('".$_POST['ts']."','yyyy-mm-dd hh24:mi:ss')
   and t.recv_time < to_date('".$_POST['te']."','yyyy-mm-dd hh24:mi:ss')  order by t.recv_time";
 $allpoint=$Db_sql->fetch_all_array($sql);
 $allstr.="[{".$arr1[$i]['PERSONNEL_SN'].":".json_encode($allpoint)."}],";
	}}
echo  substr($allstr,0,strlen($allstr)-1)."]";		
exit();		
		}
?>